// pages/main/Home.js
import api from "../../utils/api.js";
import utils from "../../utils/utils.js";
import network from "../../utils/network.js";
let app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    autoplay: true,
    interval: 5000,
    duration: 300,
    vertical: true,
    indicatorDots: true,
    circular: true,
    winWidth: 0, // 屏宽
    advBananer: ['http://image.document.guanmai.cn/1550825052828.png',
      'http://image.document.guanmai.cn/1550825052828.png'
    ],
    notice: [{
      id: 1,
      title: '公安县科技服务平台上线了'
    }, {
      id: 2,
      title: '公安县科技服务平台上线了'
    }],
    navigateItem: [{
        'typeName': '经济运行',
        'imgUrl': '/images/home5.png'
      }, {
        'typeName': '科技服务',
        'imgUrl': '/images/home6.png'
      }, {
        'typeName': '技术改造',
        'imgUrl': '/images/home7.png'
      }, {
        'typeName': '找企业',
        'imgUrl': '/images/home1.png'
      },
      /* {
            'typeName': '找科研',
            'imgUrl': '/images/home2.png'
          },*/
      {
        'typeName': '找专家',
        'imgUrl': '/images/home3.png'
      }, {
        'typeName': '找成果',
        'imgUrl': '/images/home4.png'
      }
    ], // 导航栏
    homeData: null,
    url: app.globalData.url
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.globalData.userInfo = wx.getStorageSync('jinxin_user_info');
    this.onPullDownRefresh();
    this.toast = this.selectComponent("#toast");
  },
  onShow: function () {
    if (app.globalData.userInfo) {
      let dbAllNum = wx.getStorageSync('dbAllNum')
      if(dbAllNum > 0){
        wx.setTabBarBadge({
          index: 3,
          text: dbAllNum.toString()
        })
      }

    } else {
      //移除角标
      wx.removeTabBarBadge({
        index: 3,
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    network.postJson(this, api.getHomeFrame).then(res => {
      this.setData({
        homeData: res.data.data,
        notice: res.data.data.notice
      })
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onBananerItemClick: function (e) {
    let type = e.currentTarget.dataset.type;
    let path = '/home/enterprise/EnterpriseCategory';
    if (type == '企业') {
      path = '/home/enterprise/EnterpriseCategory';
    } else if (type == '科研') {
      path = '/home/scientific/ScientificCategory';
    } else if (type == '专家') {
      path = '/home/expert/ExpertCategory';
    } else if (type == '成果') {
      path = '/home/achievement/AchievementCategory';
    } else if (type == '健康日报') {
      path = '/myself/daily/HealthRecord';
    } else {
      let enterpriseInfo = wx.getStorageSync('jinxin_enterprise_info')
      if (enterpriseInfo) {
        wx.switchTab({
          url: '/pages/main/EnterpriseEntry'
        })
        return;
      } else {
        path = '/myself/login/Login'
      }
    }
    wx.navigateTo({
      url: path
    })
  },

  /**
   * 公告
   */
  bindChange: function (e) {
    let index = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/home/notice/Notice?id=' + index
    })
  },

  onNavigateItemClick: function (e) {
    let type = e.currentTarget.dataset.type;
    let path = '/home/enterprise/EnterpriseCategory';
    switch (type) {
      case 0:
        path = '/home/news/News?modelname=newsjjyx&title=经济运行';
        break;
      case 1:
        path = '/home/news/News?modelname=newskjfw&title=科技服务';
        break;
      case 2:
        path = '/home/news/News?modelname=newsjsgz&title=技术改造';
        break;
      case 3:
        path = '/home/enterprise/EnterpriseCategory';
        break;
      case 4:
        path = '/home/expert/ExpertCategory';
        break;
      case 5:
        path = '/home/achievement/AchievementCategory';
        break;
      case 6:
        path = '/pages/main/WorkDongtai';
        break;
    }
    wx.navigateTo({
      url: path
    })
  },

  goPolicyDetails(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/policy/policy/PolicyDetails?id=' + id
    })
  },

  goEnterprise: function (e) {
    let id = e.currentTarget.dataset.id;
    let name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/home/enterprise/Enterprise?id=' + id + '&name=' + name
    })
  },

  goScientificDetails(e) {
    let id = e.currentTarget.dataset.id;
    let name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/home/scientific/ScientificDetails?id=' + id + '&name=' + name
    })
  },

  goExpert: function (e) {
    let id = e.currentTarget.dataset.id;
    let name = e.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/home/expert/Expert?id=' + id + '&name=' + name
    })
  },

  goAchievementCategory: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/home/achievement/AchievementCategory?id=' + id
    })
  },
  onQiyeGanbu: function (e) {
    wx.navigateTo({
      // url: '/pages/main/EnterpriseEntry'
      url: '/pages/main/GanbuHome'
    })
  },

  goWorkDongtaiDetails(e) {
    let id = e.currentTarget.dataset.id
    let modelname = this.data.modelname
    wx.navigateTo({
      url: '/pages/main/WorkDongtaiDetails?id=' + id + '&modelname=' + modelname
    })
  }
})